<template>
  <div>
    <div class="header">
      <div class="left">
        <img src="../assets/ld.jpg" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <div class="right">
        <el-button class="btn" @click="loginOut">退出</el-button>
      </div>
    </div>
    <div class="main">
      <div class="left">
        <div
          class="fold"
          @click="foldHandle(isCollapse)"
          :style="{ width: isCollapse ? '64px' : '200px' }"
        >
          |||
        </div>
        <div class="menuBox">
          <el-menu
            :default-active="activePath"
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
            router
          >
            <el-submenu
              :index="item.id + ''"
              v-for="(item, index) in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="icons[index]"></i>
                <span slot="title">{{ item.authName }}</span>
              </template>
              <el-menu-item
                :index="'/' + data.path"
                v-for="data in item.children"
                :key="data.id"
                @click="saveActivePath(data.path)"
                ><i class="el-icon-menu"></i>
                <span slot="title">{{ data.authName }}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </div>
      </div>
      <div class="rightBox">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isCollapse: false,
      menuList: [],
      icons: [
        "el-icon-user-solid",
        "el-icon-s-flag",
        "el-icon-s-goods",
        "el-icon-s-order",
        "el-icon-s-marketing",
      ],
      activePath: "/users",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = sessionStorage.getItem("activePath");
  },
  methods: {
    loginOut: function () {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    foldHandle(isCollapse) {
      this.isCollapse = !isCollapse;
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      this.menuList = res.data;
    },
    saveActivePath(activePath) {
      let activeUrl = "/" + activePath;
      sessionStorage.setItem("activePath", activeUrl);
      this.activePath = activeUrl;
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-right: 20px;
  height: 60px;
  color: #fff;
  .left {
    font-size: 20px;
    height: 100%;
    display: flex;
    align-items: center;
    img {
      height: 100%;
    }
    span {
      margin-left: 15px;
    }
  }
  .right {
    display: flex;
    align-items: center;
    height: 100%;

    .btn {
      background-color: #909399;
      border-color: #909399;
      color: #fff;
      &:hover {
        // background-color: #a6a9ad;
        border-color: #a6a9ad;
        color: #fff;
      }
    }
  }
}
.main {
  display: flex;
  .left {
    background-color: #333744;
    height: calc(100vh - 60px);
    .fold {
      transition: all 0.5s;
      background-color: #4a5064;
      color: #fff;
      height: 24px;
      line-height: 24px;
      text-align: center;
    }
  }
  .rightBox {
    padding: 20px;
    flex: 1;
  }
}
</style>
